﻿using System.Collections.Generic;
using System.Linq;
using ProjectSurvival.System.Save;
using QFramework;
using UnityEditor;
using UnityEngine;
using NotImplementedException = System.NotImplementedException;

namespace ProjectSurvival
{
    public class AchievementSystem:AbstractSystem
    {
        public AchievementItem Add(AchievementItem item)
        {
            Items.Add(item);
            return item;
        }
        
        public List<AchievementItem> Items = new();
        public static EasyEvent<AchievementItem> OnAchievementUnlocked = new EasyEvent<AchievementItem>();
        private SaveSystem _saveSystem;
        protected override void OnInit()
        {
            _saveSystem = this.GetSystem<SaveSystem>();
            Add(new AchievementItem()
                .WithKey("3_minutes")
                .WithName("坚持3分钟")
                .WithDescrpition("坚持3分钟\r\n奖励1000金币")
                .WithIconName("achievement_time_icon")
                .Condition(()=>Global.CurrentTime.Value>=180)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("5_minutes")
                .WithName("坚持5分钟")
                .WithDescrpition("坚持5分钟\r\n奖励1000金币")
                .WithIconName("achievement_time_icon")
                .Condition(()=>Global.CurrentTime.Value>=60*5)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("10_minutes")
                .WithName("坚持10分钟")
                .WithDescrpition("坚持10分钟\r\n奖励1000金币")
                .WithIconName("achievement_time_icon")
                .Condition(()=>Global.CurrentTime.Value>=60*10)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("15_minutes")
                .WithName("坚持15分钟")
                .WithDescrpition("坚持15分钟\r\n奖励1000金币")
                .WithIconName("achievement_time_icon")
                .Condition(()=>Global.CurrentTime.Value>=60*15)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("Lv30")
                .WithName("升级到30级")
                .WithDescrpition("第一次升级到30级\r\n奖励1000金币")
                .WithIconName("achievement_level_icon")
                .Condition(()=>Global.Level.Value>=30)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("Lv50")
                .WithName("升级到50级")
                .WithDescrpition("第一次升级到50级\r\n奖励1000金币")
                .WithIconName("achievement_level_icon")
                .Condition(()=>Global.Level.Value>=50)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_ball")
                .WithName("合成后的篮球")
                .WithDescrpition("第一次解锁合成后的篮球\r\n奖励1000金币")
                .WithIconName("paired_ball_icon")
                .Condition(()=>Global.SuperBasketBall.Value)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_boom")
                .WithName("合成后的炸弹")
                .WithDescrpition("第一次解锁合成后的炸弹\r\n奖励1000金币")
                .WithIconName("paired_bomb_icon")
                .Condition(()=>Global.SuperBomb.Value)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_knife")
                .WithName("合成后的飞刀")
                .WithDescrpition("第一次解锁合成后的飞刀\r\n奖励1000金币")
                .WithIconName("paired_simple_knife_icon")
                .Condition(()=>Global.SuperKnife.Value)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_sword")
                .WithName("合成后的剑")
                .WithDescrpition("第一次解锁合成后的剑\r\n奖励1000金币")
                .WithIconName("paired_simple_sword_icon")
                .Condition(()=>Global.SuperSword.Value)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_rotateSword")
                .WithName("合成后的守卫剑")
                .WithDescrpition("第一次解锁合成后的守卫剑\r\n奖励1000金币")
                .WithIconName("paired_rotate_sword_icon")
                .Condition(()=>Global.SuperRotateSword.Value)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            
            Add(new AchievementItem()
                .WithKey("first_time_paired_all")
                .WithName("全部能力升级")
                .WithDescrpition("第一次解锁全部能力\r\n奖励1000金币")
                .WithIconName("achievement_all_icon")
                .Condition(()=>LevelUpSystem.AllUnlockedFinish)
                .OnUnlocked(_ => { Global.Coin.Value += 1000;})
            ).Load(_saveSystem);
            //每间隔10帧监测是否有成就解锁
            ActionKit.OnUpdate.Register((() =>
            {
                if (Time.frameCount%10==0)
                {
                    foreach (var item in Items.Where(item=>!item.Unlocked&&item.ConditionCheck()))
                    {
                        item.Unlock(_saveSystem);
                    }
                }
            }));
        }
    }
}